using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace IceAge
{
    public partial class TreeMenu : DockContent 
    {
        public TreeMenu()
        {
            InitializeComponent();
        }
        //private string user_id = string.Empty;
        //public string UserId
        //{
        //    get
        //    {
        //        return user_id;
        //    }
        //}
        private void TreeMenu_Load(object sender, EventArgs e)
        {
            
        }
        public void Restart(string UserId)
        {
            CommonLibrary.USER_NAME = UserId;
            //CenturyFox.Service data_access = new CenturyFox.Service();
            //string role = CommonLibrary.DoExecute("0101", new object[2] {"hoanvk", "kijuto" }).ToString();
            DataSet menus = CommonLibrary.GetDataSetFromProcedure("p_sel_MenusByUsername", "'" + UserId + "','M'");
            treeView1.Nodes.Clear();
            if (menus != null)
            {
                foreach (DataRow row in menus.Tables[0].Rows)
                {
                    if (Convert.ToInt32(row["PARENT_ID"]) == 0)
                    {
                        TreeNode root = new TreeNode();
                        root.Text = row["MENU_TEXT"].ToString();
                        root.Name = row["MENU_ID"].ToString();                        
                        treeView1.Nodes.Add(root);
                        AddTreeNode(ref root, menus);
                    }
                }
            }
            treeView1.ExpandAll();
        }
        private void AddTreeNode(ref TreeNode parent, DataSet menus)
        {
            foreach (DataRow row in menus.Tables[0].Rows)
            {
                if (Convert.ToInt32(row["PARENT_ID"]) == Convert.ToInt32(parent.Name))
                {
                    TreeNode child = new TreeNode();
                    child.Text = row["MENU_TEXT"].ToString();
                    child.Name = row["MENU_ID"].ToString();
                    if (row["FORM_NAME"] != DBNull.Value 
                        || row["FORM_TYPE"].ToString() == "L")
                    {
                        child.Tag = new string[5] { row["FORM_NAME"].ToString(), row["PARAMETER"].ToString()
                        , row["DOCK"].ToString(), row["FORM_TYPE"].ToString(), row["READ_ONLY"].ToString()};                        
                        child.StateImageIndex = 1;
                    }
                    else
                    {
                        child.StateImageIndex = 0;
                    }
                    
                    parent.Nodes.Add(child);
                    AddTreeNode(ref child, menus);
                }                
            }
        }
       
        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            try
            {
                if (e.Node.Nodes.Count > 0)
                    return;
                if (e.Node.Tag != null)
                {
                    string[] node_tag = (string[])e.Node.Tag;

                    //Single instance
                    if (node_tag[3] == "S")
                    {
                        foreach (DockContent childForm in this.DockPanel.Contents)
                        {
                            if (childForm.GetType().ToString() == node_tag[0])
                            {
                                childForm.Text = e.Node.Text;
                                childForm.Tag = node_tag[1];
                                childForm.Activate();
                                return;
                            }
                        }
                    }
                    else if (node_tag[3] == "D") //Dialog
                    {
                        Form modalForm = (Form)Activator.CreateInstance(
                        Type.GetType(node_tag[0], true, true));
                        modalForm.Text = e.Node.Text;
                        modalForm.Tag = node_tag[1];
                        modalForm.ShowDialog();
                        return;
                    }
                    //if (node_tag[3] == "L") //Form list
                    //{
                    //    GridForm listForm = new GridForm();
                    //    listForm.Text = e.Node.Text;
                    //    listForm.Tag = node_tag[1];
                    //    listForm.parameters = new object[2] { node_tag[0].ToString(), node_tag[4].ToString() };                        
                    //    listForm.Show(this.DockPanel, DockState.Document);
                    //    return;
                    //}

                    //General form
                    DockContent obj = (DockContent)Activator.CreateInstance(
                        Type.GetType(node_tag[0], true, true));
                    obj.Text = e.Node.Text;
                    obj.Tag = node_tag[1];
                    obj.Show(this.DockPanel, DockState.Document);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
            
        }
    }
}